package com.js.sys.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.js.lib_common.event.StartBrotherEvent;
import com.js.lib_common.event.TabSelectedEvent;
import com.js.lib_common.view.BottomBar;
import com.js.lib_common.view.BottomBarTab;
import com.js.sys.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;


public class MainFragment extends SupportFragment {

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FORTH = 3;
    public static final int FIFTH = 4;
    @BindView(R.id.bottomBar)
    BottomBar bottomBar;
    Unbinder unbinder;

    private SupportFragment[] mFragments = new SupportFragment[5];


    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(FirstTabFragment.class);
        if (firstFragment == null) {
            mFragments[FIRST] = FirstTabFragment.newInstance();
            mFragments[SECOND] = SecondTabFragment.newInstance();
            mFragments[THIRD] = ThirdTabFragment.newInstance();
            mFragments[FORTH] = ForthTabFragment.newInstance();
            mFragments[FIFTH] = FifthTabFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FORTH],
                    mFragments[FIFTH]);
        } else {
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findChildFragment(SecondTabFragment.class);
            mFragments[THIRD] = findChildFragment(ThirdTabFragment.class);
            mFragments[FORTH] = findChildFragment(ForthTabFragment.class);
            mFragments[FIFTH] = findChildFragment(FifthTabFragment.class);

        }
    }

    private void initView() {
        bottomBar
                .addItem(new BottomBarTab(_mActivity, R.drawable.tab_firs_selector, getString(R.string.tab_first)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.tab_second_selector, getString(R.string.tab_second)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.tab_third_selector, getString(R.string.tab_third)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.tab_forth_selector, getString(R.string.tab_forth)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.tab_forth_selector, getString(R.string.tab_fifth)));

        bottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);
//                BottomBarTab tab = bottomBar.getItem(FIFTH);
//                if (position == FIFTH) {
//                    tab.setUnreadCount(0);
//                } else {
//                    tab.setUnreadCount(tab.getUnreadCount() + 1);
//                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                // 在FirstPagerFragment,FirstHomeFragment中接收, 因为是嵌套的Fragment
                // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
                EventBusActivityScope.getDefault(_mActivity).post(new TabSelectedEvent(position));
            }
        });


    }



    /**
     * start other BrotherFragment
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void startFragment(StartBrotherEvent event) {
        start(event.targetFragment);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }


}
